package JUnitTests;

import static org.junit.Assert.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import DataStorageClasses.Employee;
import Database.DatabaseConnector;
import ExceptionClasses.InvalidDateBoundException;

public class EmployeeConnectionTests 
{
	private static DatabaseConnector dbConnector;
	private static String businessName;
	private static String businessPassword;
	private static String employeeName;
	private static long id;
	
	@BeforeClass static public void initialize()
	{
		dbConnector = new DatabaseConnector();
		dbConnector.connect();
		
		businessName = "TesterBusiness";
		businessPassword = "password";
		employeeName = "Sam";
		dbConnector.addBusiness(businessName, businessPassword, null, null, null, null, null, null, null, null,null);
		
		Employee employee = dbConnector.addEmployee(businessName, employeeName, null, null, null, -1, -1, null, null, null, null, null, null);
		id = employee.getEmployeeID();
	}
	
	@AfterClass static public void destroy()
	{
		dbConnector.removeEmployee(id);
		dbConnector.removeBusiness(businessName, businessPassword);
		dbConnector.disconnect();
	}
	
	@Test public void testAddEmployee()
	{
		Employee employee = dbConnector.addEmployee("wrongbus", null, null, null, null, -1, -1, null, null, null, null, null, null);
		assertTrue(employee == null);
	}
	
	@Test public void testUpdateEmployee()
	{
		String newFirst = "new first";
		String newLast = "new last";
		Date newStart = new Date(0);
		Date newFinish = new Date(0);
		double newWage = 12;
		long newSIN = 23423;
		String newStreet = "new street";
		String newCity = "new city";
		String newProvince = "new province";
		String newPostal = "new postal";
		String newHome = "newhome";
		String newCell = "newcell";
		
		boolean updated = dbConnector.updateEmployee(id, newFirst, newLast, newStart, newFinish, newWage, newSIN, newStreet,
				newCity, newProvince, newPostal, newHome, newCell);
		
		assertTrue(updated);
		
		Employee employee = dbConnector.getEmployee(id, businessName);
		
		assertArrayEquals(newFirst.toCharArray(), employee.getFirstName().toCharArray());
		assertArrayEquals(newLast.toCharArray(), employee.getLastName().toCharArray());
		assertArrayEquals(newStart.toString().toCharArray(), employee.getDateHired().toString().toCharArray());
		assertArrayEquals(newFinish.toString().toCharArray(), employee.getDateFired().toString().toCharArray());
		assertTrue(newWage == employee.getWage());
		assertTrue(newSIN == employee.getSIN());
		assertArrayEquals(newStreet.toCharArray(), employee.getStreet().toCharArray());
		assertArrayEquals(newCity.toCharArray(), employee.getCity().toCharArray());
		assertArrayEquals(newProvince.toCharArray(), employee.getProvince().toCharArray());
		assertArrayEquals(newPostal.toCharArray(), employee.getPostalCode().toCharArray());
		assertArrayEquals(newHome.toCharArray(), employee.getHomeTel().toCharArray());
		assertArrayEquals(newCell.toCharArray(), employee.getCell().toCharArray());
		
		updated = dbConnector.updateEmployee(-1, newFirst, newLast, newStart, newFinish, newWage, newSIN, newStreet,
				newCity, newProvince, newPostal, newHome, newCell);
		
		assertFalse(updated);
	}
	
	@Test public void testGetEmployee()
	{
		Employee employee = dbConnector.getEmployee(id, "wrongbusiness");
		assertTrue(employee == null);
		
		employee = dbConnector.getEmployee(-1, businessName);
		assertTrue(employee == null);
		
		employee = dbConnector.getEmployee(id, businessName);
		assertTrue(employee != null);
	}
	
	@Test public void testRemoveEmployee()
	{
		boolean removed = dbConnector.removeEmployee(-1);
		assertFalse(removed);
	}
	
	@Test public void testGetEmployeesOnJob()
	{
		long clientID = dbConnector.addClient("tester", "client", null, null, null, null, null, null, null, null, businessName).getClientId();
		long jobID = dbConnector.addNewJob(clientID, 0, 0, 0, null, null, null, null, null, null, null, businessName).getJobID();
		long employee = dbConnector.addEmployee(businessName, "some other", "last", null, null, 12, 232, null, null, null, null, null, null).getEmployeeID();
		Calendar cal = Calendar.getInstance();
		cal.set(2011, 02, 23);
		Date date = new Date(cal.getTimeInMillis());
		
		dbConnector.addTimeSheet(jobID, id, date, 12, false);
		
		ArrayList<Employee> employees = dbConnector.getEmployeesOnJob(jobID);
		assertTrue(employees.size() == 1);
		assertTrue(employees.get(0).getEmployeeID() == id);
		
		dbConnector.removeTimeSheet(id, jobID, date);
		dbConnector.removeJob(jobID);
		dbConnector.removeEmployee(employee);
		dbConnector.removeClient(clientID);
	}
	
	@Test public void testGetEmployees()
	{
		String newFirst = "new first";
		String newLast = "new last";
		Date newStart = new Date(0);
		Date newFinish = new Date(0);
		double newWage = 12;
		long newSIN = 23423;
		String newStreet = "new street";
		String newCity = "new city";
		String newProvince = "new province";
		String newPostal = "new postal";
		String newHome = "newhome";
		String newCell = "newcell";
		
		dbConnector.addEmployee(businessName, newFirst, newLast, newStart, newFinish, newWage, newSIN, newStreet,
				newCity, newProvince, newPostal, "asdf", newCell);
		boolean updated = dbConnector.updateEmployee(id, newFirst, newLast, newStart, newFinish, newWage, newSIN, newStreet,
				newCity, newProvince, newPostal, newHome, newCell);
		
		assertTrue(updated);
		
		try {
			ArrayList<Employee> employees = dbConnector.getEmployees(businessName, newFirst, newLast, null, null, null, null, newWage, newSIN, newStreet,
					newCity, newProvince, newPostal, newHome, newCell, false);
			assertTrue(employees != null);
			
			assertTrue(employees.size() == 1);
		
			Employee employee = employees.get(0);
			
			assertArrayEquals(newFirst.toCharArray(), employee.getFirstName().toCharArray());
			assertArrayEquals(newLast.toCharArray(), employee.getLastName().toCharArray());
			assertArrayEquals(newStart.toString().toCharArray(), employee.getDateHired().toString().toCharArray());
			assertArrayEquals(newFinish.toString().toCharArray(), employee.getDateFired().toString().toCharArray());
			assertTrue(newWage == employee.getWage());
			assertTrue(newSIN == employee.getSIN());
			assertArrayEquals(newStreet.toCharArray(), employee.getStreet().toCharArray());
			assertArrayEquals(newCity.toCharArray(), employee.getCity().toCharArray());
			assertArrayEquals(newProvince.toCharArray(), employee.getProvince().toCharArray());
			assertArrayEquals(newPostal.toCharArray(), employee.getPostalCode().toCharArray());
			assertArrayEquals(newHome.toCharArray(), employee.getHomeTel().toCharArray());
			assertArrayEquals(newCell.toCharArray(), employee.getCell().toCharArray());
		} catch (InvalidDateBoundException e) {
			e.printStackTrace();
		}
	}
}
